<script language="javascript" type="text/javascript">

var g_install_timeout = 30;
var g_ddmip_ajax_ret = null;
var g_ddmip_cur_step = 1;
var g_start_install_time = $.now();
var g_valid_status = [ <?cs var:config.valid_status ?> ];
var g_ddmip_cur_mach_info = <?cs var:config.cur_machine_info ?>;

function ddmipShowNewStatusStep(js)
{
    // 进度未变化
    if(g_ddmip_cur_step == js.proc_status)
        return;

    // 安装结束并成功
    if(js.proc_status == 0)
        js.proc_status = <?cs var:config.max_install_proc ?>;

    // 进度已变化，显示并重新开始计时
    while(js.proc_status <= <?cs var:config.max_install_proc ?> && g_ddmip_cur_step < js.proc_status) {
        g_ddmip_cur_step++;
        if(g_valid_status.indexOf(g_ddmip_cur_step) != -1)
            dmtAddPluginStatusShow(g_ddmip_cur_step, 'ddip_install_process', 'ddmip_result');
    }

    if(js.proc_status < <?cs var:config.max_install_proc ?>) {
        g_install_timeout = 30;
        $('#ddip_timeout').text(''+g_install_timeout);
    }
    else {
        // 部署出错
        if(js.proc_status > <?cs var:config.max_install_proc ?>)
            dmtAddPluginStatusShow(js.proc_status, 'ddip_install_process', 'ddmip_result');
        else {
            var tw = ($.now() - g_start_install_time)/1000;
            $('#ddmip_result').html('<font color="blue">部署成功，总共耗时：' + dmtGetHumanTime(tw)); 
        }
        clearInterval(g_addplugin_timerid);
        g_addplugin_timerid = 0;
    }
}

function ddmipInnerModeUploadPluginPack(js)
{
    var reqpara = new Object();
    reqpara.action = 'inner_upload_plugin_pack_info';
    reqpara.machine_id = <?cs var:config.machine_id ?>;
    reqpara.plugin_id = <?cs var:config.plugin_id ?>;
    reqpara.self_domain = window.document.domain;
    reqpara.plugin_version = '<?cs var:config.plugin_ver ?>';

    js.plugin_name = '<?cs var:config.plugin_name ?>';
    reqpara.plugin_pack = JSON.stringify(js);

    $.ajax({ 
        type: 'post',
        url: '<?cs var:config.cgipath?>mt_slog',
        data: reqpara,
        global: false,
	    dataType: 'json', 
        success: function(js) {
            if(dmtFirstDealAjaxResponse(js))
                return;

            if(js.ret != 0) {
                clearInterval(g_addplugin_timerid);
                g_addplugin_timerid = 0;
                $('#ddmip_result').html('<font color="red">上传插件部署包到本地web服务器失败</font>');
            }
            else {
                clearInterval(g_addplugin_timerid);
                dmtAddPluginStatusShow(1, 'ddip_install_process', 'ddmip_result');
                g_install_timeout = 30;
                g_addplugin_timerid = setInterval(ddipInstallTimeout, 1000);
            }
        }
    });
}

function ddmipInnerModeDealDownUrl(jsurl)
{
    jsurl.action = 'inner_deal_download_url';
    jsurl.machine_id = <?cs var:config.machine_id ?>;
    jsurl.plugin_id = <?cs var:config.plugin_id ?>;
    $.ajax({ 
        type: 'post',
        url: '<?cs var:config.cgipath?>mt_slog',
        data: jsurl,
        global: false,
	    dataType: 'json', 
        success: function(js) {
            if(dmtFirstDealAjaxResponse(js))
                return;
            if(js.ret != 0) {
                clearInterval(g_addplugin_timerid);
                g_addplugin_timerid = 0;
                $('#ddmip_result').html('<font color="red">内网部署失败，处理云端下载地址失败');
            }
            else {
                if(typeof jsurl.download_str == 'string')
                    js.download_str = jsurl.download_str;
                ddmipInnerModeDownloadPack(js);
            }
        }
    });
}

function ddmipInnerModeDownloadPack(jsurl)
{
	var requrl = "<?cs var:config.xrkmonitor_url?>/cgi-bin/mt_slog_open?action=download_plugin";
	requrl += "&plugin=<?cs var:config.plugin_id ?>";
	requrl += "&user=<?cs var:config.xrkmonitor_account ?>";
	requrl += "&enc_str_len=" + jsurl.enc_str_len;
	requrl += "&dec_str_len=" + jsurl.dec_str_len;
	requrl += "&access_str=" + encodeURIComponent(jsurl.access_str);

	$.ajax({
		type: 'get',
		url : requrl,
		dataType: 'json',
		global: false,
		timeout : function() { 
            $('#ddmip_result').html('<font color="red">内网部署失败，从云端下载插件部署包超时</font>');
            clearInterval(g_addplugin_timerid);
            g_addplugin_timerid = 0;
		},
		success: function(js) {
            if(js.ret != 0) {
                if(typeof js.msg == 'string')
                    $('#ddmip_result').html('<font color="red">从云端下载插件部署包失败：<br />'+js.msg + '</font>');
                else 
                    $('#ddmip_result').html('<font color="red">从云端下载插件部署包失败，错误码：'+js.ret+'</font>');
                clearInterval(g_addplugin_timerid);
                g_addplugin_timerid = 0;
            }
            else {
                dmtAddPluginStatusShow(17, 'ddip_install_process', 'ddmip_result');
                g_install_timeout = 60;
                if(typeof jsurl.download_str == 'string')
                    js.download_str = jsurl.download_str;
                ddmipInnerModeUploadPluginPack(js);
            }
		}
	});
}

function ddmipInnerModeGetDownloadUrl()
{
	var requrl = "<?cs var:config.xrkmonitor_url?>/cgi-bin/mt_slog_open?action=open_preinstall_plugin_enc";
	requrl += "&plugin=<?cs var:config.plugin_id ?>";
	requrl += "&user=<?cs var:config.xrkmonitor_account ?>";
	requrl += "&os=" + g_ddmip_cur_mach_info.os;
	requrl += "&os_arc=" + g_ddmip_cur_mach_info.os_arc;
	requrl += "&agent_ver=" + g_ddmip_cur_mach_info.agent_ver;
	requrl += "&libc_ver=" + g_ddmip_cur_mach_info.libc_ver;
	requrl += "&libcpp_ver=" + g_ddmip_cur_mach_info.libcpp_ver;

	$.ajax({
		type: 'get',
		url : requrl,
		dataType: 'json',
		global: false,
		timeout : function() { 
            $('#ddmip_result').html('<font color="red">内网部署失败，ajax 请求超时</font>');
            clearInterval(g_addplugin_timerid);
            g_addplugin_timerid = 0;
		},
		success: function(js) {
            if(js.ret != 0) {
                if(typeof js.msg == 'string')
                    $('#ddmip_result').html('<font color="red">内网部署失败，云端请求失败：'+js.msg);
                else 
                    $('#ddmip_result').html('<font color="red">内网部署失败，云端请求失败，错误码：'+js.ret);
                clearInterval(g_addplugin_timerid);
                g_addplugin_timerid = 0;
            }
            else {
                dmtAddPluginStatusShow(16, 'ddip_install_process', 'ddmip_result');
                g_install_timeout = 60;
                ddmipInnerModeDealDownUrl(js);
            }
		}
	});
}

function ddipInnerInstallTimeout()
{
    g_install_timeout--;
    $('#ddip_timeout').text(''+g_install_timeout);
    if(g_install_timeout <= 0)
    {
        clearInterval(g_addplugin_timerid);
        g_addplugin_timerid = 0;
        $('#ddmip_result').html('<font color="red">内网部署失败，部署进度60秒未有变化</font>');
    }
}

function ddipInstallTimeout()
{
    g_install_timeout--;
    $('#ddip_timeout').text(''+g_install_timeout);
    if(g_install_timeout <= 0)
    {
        clearInterval(g_addplugin_timerid);
        g_addplugin_timerid = 0;
        $('#ddmip_result').html('<font color="red">部署进度30秒未有变化</font><br>'
            + '<font color="red">您可以稍后在上报机器管理中查询部署是否成功</font>');
    }
    else {
        if(g_ddmip_ajax_ret == null || g_ddmip_ajax_ret == 1) {
	        var para = new Object();
            para.action = 'refresh_preinstall_plugin_status';
            para.machine_id = <?cs var:config.machine_id ?>;
            para.plugin_id = <?cs var:config.plugin_id ?>;
            var requrl = '<?cs var:config.cgipath?>mt_slog';
            $.ajax({ type: 'GET',
                url: requrl,
                data: para,
                global: false,
		        dataType: 'json', 
                success: function(js) {
                    g_ddmip_ajax_ret = 1;
                    if(dmtFirstDealAjaxResponse(js))
                        return;
                    if(js.ret != 0) {
                        clearInterval(g_addplugin_timerid);
                        g_addplugin_timerid = 0;
                        $('#ddmip_result').html('<font color="red">ajax 请求返回错误:' + js.msg + '，部署失败！</font>');
                    }
                    else {
                        ddmipShowNewStatusStep(js);
                    }
                },
                error: function() {
                    clearInterval(g_addplugin_timerid);
                    g_addplugin_timerid = 0;
                    $('#ddmip_result').html('<font color="red">ajax 请求失败，部署失败！</font>');
                }
            });
            g_ddmip_ajax_ret = 0;
        }
    }
}

$(document).ready(function(){
    // g_addplugin_timerid define in dmt.addplugin.js 
    if(g_addplugin_timerid != 0) {
        clearInterval(g_addplugin_timerid);
        g_addplugin_timerid = 0;
    }

    <?cs if:config.install_proc > config.max_install_proc ?>
        <?cs if:config.has_last_work == 1?>
        $('#ddmip_result').html('<font color="red">您最近5分钟有相同部署操作，请稍后再试！' +
            '<br />最近部署时间：<?cs var:config.last_install_time ?></font>');
        <?cs else ?>
        $('#ddmip_result').html('<font color="red">部署任务发布失败，部署失败！</font>');
        <?cs /if ?>
    <?cs elif:config.is_inner_mod ?>
        dmtAddPluginStatusShow(15, 'ddip_install_process', 'ddmip_result');
        g_install_timeout = 60;
        g_addplugin_timerid = setInterval(ddipInnerInstallTimeout, 1000);
        ddmipInnerModeGetDownloadUrl();  
    <?cs else ?>
        dmtAddPluginStatusShow(1, 'ddip_install_process', 'ddmip_result');
        g_addplugin_timerid = setInterval(ddipInstallTimeout, 1000);
    <?cs /if ?>
});

</script>

<div class="pageContent">
	<div class="pageFormContent" layoutH='0'>
        <table>
        <tr>
        <td rowspan='2'>
        <div style='height:280px;width:300px;'>
            <h3>部署进度</h3>
            <br />
            <ol id='ddip_install_process' style='list-style:decimal !important; margin-left:10px'></ol>
            <br />
            插件部署中请稍等：<span id='ddip_timeout'><?cs var:config.show_timer_end ?></span> ...
            <br />
            <br />
            <span id='ddmip_result'></span>
        </div>
        </td>

        <td> 
        <div style='height:100px;width:250px; float:right'>
            <h3>待部署机器</h3>
            <br />
            <label style='width:60px'>机器名: </label><?cs var:config.machine_name ?>
            <br />
            <label style='width:60px'>机器IP: </label><?cs var:config.machine_ip ?>
            <br />
            <label style='width:60px'>机器ID: </label><?cs var:config.machine_id ?>
            <br />
        </div>
        </td>
        </tr>

        <tr>
        <td>
        <div style='height:180px;width:250px;float:right'>
            <h3>待部署插件</h3>
            <br />
            <label style='width:80px'>插件显示名: </label><?cs var:config.plugin_show_name ?>
            <br />
            <label style='width:80px'>插件部署名: </label><?cs var:config.plugin_name ?>
            <br />
            <label style='width:80px'>部署版本: </label><?cs var:config.plugin_ver ?>
            <br />
            <label style='width:80px'>插件ID: </label><?cs var:config.plugin_id ?>
        </div>
        </td>
        </tr>

        <tr>
        <td colspan='2'>
        <div style='height:60px;width:500px; text-align:left' class='note-text'>
            <h3>提示</h3>
            1. 插件默认部署在 agent 目录中的 xrkmonitor_plugin 目录下，您可以迁移至其它目录 <br />
            2. 一键部署都是以默认配置方式部署，您可以稍后在机器或者插件中修改配置<br />
            3. 如插件部署失败可查看 agent plugin_install_log 目录下对应插件的部署日志<br />
        </div>
        </td>
        </tr>
        </table>
	</div>

	<div class="formBar">
		<ul style='list-style:none'>
			<li><button type="button" class="buttonActive close">关闭</button></li>
		</ul>
	</div>
</div>


